package com.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IManagerAuthService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("auth")
public class ManagerAuthController {

	@Autowired
	private IManagerAuthService managerAuthService;
	
	@RequestMapping("")
	public ModelAndView configPage(){
		ModelAndView mv = new ModelAndView("auth/managerList");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response){
		DataVector<Map<String, Object>> listData = managerAuthService.getList(new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@RequestMapping("add/{type}")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type) {
		String userLevel = SessionUtil.getStringAttri(request, SessionData.levelKey);
		ModelAndView mv = null;
		if (!"9".equals(userLevel)) {
			mv = new ModelAndView("common/empty");
			mv.addObject("pageInfo", "对不起，您没有此权限！");
			return mv;
		}
		String titleName = null;
		if (type.equals(9)) {
			titleName = "添加超级管理员";
		}else if (type.equals(8)) {
			titleName = "添加公司管理员";
		}else if (type.equals(7)) {
			titleName = "添加财务总管";
		}else {
			titleName = "";
		}
		
		mv = new ModelAndView("auth/userList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("titleName", titleName);
		mv.addObject("type", type);
		return mv;
	}
	@RequestMapping("save/{type}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		String ids = request.getParameter("ids");
		Boolean isSuccess = managerAuthService.add(ids, type);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Boolean isSuccess = managerAuthService.delete(ids);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	
}
